package com.tudouji.project.product.service.impl;

import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.project.product.domain.Salesspecification;
import com.tudouji.project.product.mapper.CategoryMapper;
import com.tudouji.project.product.mapper.ProductinfoMapper;
import com.tudouji.project.product.mapper.SalesspecificationMapper;
import com.tudouji.project.product.service.ISalesspecificationService;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 产品规格Service业务层处理
 *
 * @author wf
 * @date 2020-12-30
 */
@Service
public class SalesspecificationServiceImpl implements ISalesspecificationService
{
    @Autowired
    private SalesspecificationMapper salesspecificationMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductinfoMapper productinfoMapper;
    /**
     * 查询产品规格
     *
     * @param guid 产品规格ID
     * @return 产品规格
     */
    @Override
    public Salesspecification selectSalesspecificationById(String guid)
    {
        return salesspecificationMapper.selectSalesspecificationById(guid);
    }

    /**
     * 根据金蝶编码查询产品规格
     *
     * @param kingdeenum
     * @return
     */
    public List<Salesspecification> selectSalesspecificationBykingdeenum(String kingdeenum){
        return this.salesspecificationMapper.selectSalesspecificationBykingdeenum(kingdeenum);
    }

    /**
     * 查询产品规格列表
     *
     * @param salesspecification 产品规格
     * @return 产品规格
     */
    @Override
    public List<Salesspecification> selectSalesspecificationList(Salesspecification salesspecification)
    {
        return salesspecificationMapper.selectSalesspecificationList(salesspecification);
    }
    @Override
    public List<Salesspecification> selectSalesspecificationListPC(Salesspecification salesspecification)
    {
        return salesspecificationMapper.selectSalesspecificationListPC(salesspecification);
    }


    /**
     * 新增产品规格
     *
     * @param salesspecification 产品规格
     * @return 结果
     */
    @Override
    public int insertSalesspecification(Salesspecification salesspecification)
    {
        salesspecification.setName(salesspecification.getName().trim().toLowerCase());
        salesspecification.setGuid(IdUtils.fastSimpleUUID());
        salesspecification.setOperator(SecurityUtils.getLoginUser().getUser().getNickName());
        salesspecification.setOperatorid(SecurityUtils.getLoginUser().getUser().getUserId().toString());
        salesspecification.setCreatetime(DateUtils.getNowDate());
        salesspecification.setLastupdate(DateUtils.getNowDate());
        return salesspecificationMapper.insertSalesspecification(salesspecification);
    }

    /**
     * 修改产品规格
     *
     * @param salesspecification 产品规格
     * @return 结果
     */
    @Override
    public int updateSalesspecification(Salesspecification salesspecification)
    {
        salesspecification.setLastupdate(DateUtils.getNowDate());
        return salesspecificationMapper.updateSalesspecification(salesspecification);
    }

    /**
     * 批量删除产品规格
     *
     * @param guids 需要删除的产品规格ID
     * @return 结果
     */
    @Override
    public int deleteSalesspecificationByIds(String[] guids)
    {
        return salesspecificationMapper.deleteSalesspecificationByIds(guids);
    }

    /**
     * 删除产品规格信息
     *
     * @param guid 产品规格ID
     * @return 结果
     */
    @Override
    public int deleteSalesspecificationById(String guid)
    {
        return salesspecificationMapper.deleteSalesspecificationById(guid);
    }

    /**
     * 判断产品是否使用
     *
     * @param guid
     * @return
     */
    public boolean checkSpguid(String guid){
        Integer sellNum = this.salesspecificationMapper.getSellItemCountBySpguid(guid);
        if(sellNum > 0){
            return true;
        }else{
            Integer whNum = this.salesspecificationMapper.getWhinapplyitemCountBySpguid(guid);
            if(whNum > 0){
                return true;
            }else{
                Integer tranNum = this.salesspecificationMapper.getTransferapplyitemCountBySpguid(guid);
                if(tranNum > 0){
                    return true;
                }else{
                    return false;
                }

            }
        }
    }

    @Override
    public boolean checkBaseFlag(String guid, String proGuid, Integer type) {
        if (1 == type){
            Salesspecification spec = new Salesspecification();
            if(guid != null){
                spec.setGuid(guid);
            }
            spec.setProductguid(proGuid);
            spec.setType(type);
            spec.setBaseFlag(1);
            List<Salesspecification> specs = salesspecificationMapper.selectSalesspecificationList(spec);
            if (specs.size() > 0){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Salesspecification> getSpecs(String productGuid, Integer valuation, String stats, String brandid
            , Integer flag, String name, Integer type) {

        Salesspecification spec = new Salesspecification();
        if(StringUtils.isNotEmpty(productGuid)){
            spec.setProductguid(productGuid);
        }
        if(null != valuation){
            spec.setValuation(valuation);
        }
        if (StringUtils.isNotBlank(stats)) {
            String[] sts = stats.split(",");
            Integer[] s = (Integer[]) ConvertUtils.convert(sts, Integer.class);
            spec.setStats(s);
        }
        if(StringUtils.isNotEmpty(brandid)){
            spec.setBrandid(brandid);
        }
        if(null != flag){
            spec.setFlag(flag);
        }
        if(StringUtils.isNotEmpty(name)){
            spec.setName(name);
        }
        if(null != type){
            spec.setType(type);
        }
        return salesspecificationMapper.selectSalesspecificationList(spec);
    }

    /**
     * @Author shaodong
     * @Description //原料出入库调用此方法
     * @Date 18:37 2021/2/3
     * @Param  通过productinfoid
     * @return 返回baseFlag=1的原材料规格
     **/
    @Override
    public Salesspecification getBaseMatril(String productinfoid){
        Salesspecification spec = new Salesspecification();
        spec.setBaseFlag(1);
        spec.setStatus(1);
        spec.setType(1);
        spec.setProductguid(productinfoid);
        return salesspecificationMapper.selectSalesspecification(spec);
    }
}
